﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using Antu.WorkFlowFactory;

namespace WebSys.UI.Control.ZhengShou
{
    public class EditUser2Control : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        TextBox txtLOGINID, txtNAME, txtPWD, rpwd,
            txtISCOMUSER, txtTHISIP, txtISMODNAME, txtID;
        CheckBox ckbstatus, ckbISOA;
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            string strid = base.Page.Request["ID"];
            string action = base.Page.Request["action"];
            
            if (!base.Page.IsPostBack) {
                txtTHISIP.Text = base.Page.Request.UserHostAddress;
                if (string.IsNullOrEmpty(strid)) { txtID.Text = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper(); }
                else
                {
                    txtID.Text = strid;
                    EluZSProvider.User user = _EluZSDataProvider.GetUser(strid);
                    txtLOGINID.Text = user.Loginid;
                    txtNAME.Text = user.Name;
                    txtPWD.Text = user.Pwd;
                    rpwd.Text = user.Pwd;
                    if (user.Status == "1") { ckbstatus.Checked = true; } else { ckbstatus.Checked = false; }
                    if (user.ISOA == "1") { ckbISOA.Checked = true; } else { ckbISOA.Checked = false; }
                }
            }
            if (!string.IsNullOrEmpty(action) && action == "del")
            {
                if (!string.IsNullOrEmpty(strid))
                {
                    bool del = _EluZSDataProvider.DeleteXmByID("zs_user", "id", strid);
                    if (del) { base.Page.Response.Redirect("User.aspx"); }
                }
            }
            if (base.Page.Session["UserID"] != null)
            {
                string userid = base.Page.Session["UserID"].ToString();
                if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid))
                {
                    txtLOGINID.Enabled = false; txtNAME.Enabled = false;
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "showor", "<script>showor();</script>");
                }
            }
            else {
                Antu.Utilities.UtilitiesPage.AlertDialog("请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            
            Button btn_Insert = (Button)FindControl("btn_Insert");
            btn_Insert.Click += new EventHandler(btn_Insert_Click);
        }
        protected void InitControl() {
            txtLOGINID = (TextBox)FindControl("txtLOGINID");
            txtNAME = (TextBox)FindControl("txtNAME");
            txtPWD = (TextBox)FindControl("txtPWD");
            rpwd = (TextBox)FindControl("rpwd");
            txtISCOMUSER = (TextBox)FindControl("txtISCOMUSER");

            txtTHISIP = (TextBox)FindControl("txtTHISIP");
            txtISMODNAME = (TextBox)FindControl("txtISMODNAME");
            txtID = (TextBox)FindControl("txtID");
            ckbstatus = (CheckBox)FindControl("ckbstatus");
            ckbISOA = (CheckBox)FindControl("ckbISOA");
        }

        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            string strid = base.Page.Request["ID"];
            EluZSProvider.User user = new EluZSProvider.User();
            if (!string.IsNullOrEmpty(strid)) { user = _EluZSDataProvider.GetUser(strid); }
            user.Id = txtID.Text;
            user.Loginid = txtLOGINID.Text;
            user.Name = txtNAME.Text;
            user.Pwd = txtPWD.Text;
            user.Addtime = (DateTime?)System.DateTime.Now;
            //user.Lastdate = (DateTime?)System.DateTime.Now;
            //user.Lastip = base.Page.Request.UserHostAddress;
            user.Thisip = txtTHISIP.Text;
            user.Iscomuser = txtISCOMUSER.Text;
            user.Ismodname = txtISMODNAME.Text;
            if (ckbstatus.Checked) { user.Status = "1"; } else { user.Status = "0"; }
            if (ckbISOA.Checked) { user.ISOA = "1"; } else { user.ISOA = "0"; }
            bool save = _EluZSDataProvider.SaveUser(strid, user);
            if (save) {
                _FlowFactoryProvider.updateStUser(user.ISOA, user.Id, user.Name, user.Loginid, user.Pwd);
                base.Page.Response.Redirect("user.aspx"); 
            }
        }
        
    }
}


